import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:taonile/config/config.dart';
import 'package:taonile/entity/query_express_info_entity.dart';
import 'package:webview_flutter/webview_flutter.dart';

import '../entity/order_detail_entity.dart';
import '../entity/receipt_address_entity.dart';
import '../http/api.dart';
import '../routers/routers.dart';
import '../views/default_views.dart';
import '../views/dialog_manager.dart';

///
/// 查询物流进度
///
class OrderQueryExpress extends StatefulWidget {
  String orderNo;

  OrderQueryExpress({Key? key, this.orderNo = ''}) : super(key: key);

  @override
  State<OrderQueryExpress> createState() => _OrderQueryExpressState();
}

class _OrderQueryExpressState extends State<OrderQueryExpress> {
  late WebViewController controller;

  QueryExpressInfoEntity entity = QueryExpressInfoEntity();
  String reciverAddress = '';

  @override
  void initState() {
    super.initState();

    controller = WebViewController();
    controller.setJavaScriptMode(JavaScriptMode.unrestricted);
    controller.setBackgroundColor(const Color(0x00000000));
    controller.enableZoom(true);

    getData();
    orderDetail();
  }

  getData() async {
    dynamic result = await Api.expressMap(widget.orderNo);
    setState(() {
      entity = QueryExpressInfoEntity.fromJson(result);
      controller.loadRequest(Uri.parse(entity.data.trailUrl));
    });
  }

  orderDetail() async {
    dynamic result = await Api.orderDetail(widget.orderNo);
    setState(() {
      OrderDetailEntity entity = OrderDetailEntity.fromJson(result);
      reciverAddress = entity.data.addressSnap.address;
    });
  }

  Widget getListItem(int index) {
    return Container(
      // height: 120.w,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            width: 40.w,
            alignment: Alignment.center,
            child: Column(
              children: [
                Visibility(
                  visible: index == 0,
                  child: Container(
                    width: 20.w,
                    height: 20.w,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(10.w)),
                      color: Config.colorMain,
                    ),
                  ),
                ),
                Visibility(
                  visible: index != 0,
                  child: Container(
                    width: 13.w,
                    height: 13.w,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(10.w)),
                      color: Config.colorText3,
                    ),
                  ),
                ),
                Container(
                  margin: EdgeInsets.symmetric(vertical: 5.w),
                  width: 1.5.w,
                  height: 120.w,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.all(Radius.circular(0.w)),
                    color: Config.colorText3,
                  ),
                ),
              ],
            ),
          ),
          Container(
            width: 600.w,
            padding: EdgeInsets.only(bottom: 15.w),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  entity.data.data[index].status,
                  style: TextStyle(fontSize: 25.sp, height: 1.0, color: Config.colorText1),
                ),
                Text(
                  entity.data.data[index].context,
                  style: TextStyle(fontSize: 22.sp, height: 1.5, color: Config.colorText1),
                ),
                Text(
                  entity.data.data[index].ftime,
                  style: TextStyle(fontSize: 22.sp, height: 1.5, color: Config.colorText3),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Config.colorBg2,
      body: SizedBox(
        width: double.infinity,
        height: double.infinity,
        child: Stack(
          children: [
            Container(
              width: double.infinity,
              height: double.infinity,
              margin: EdgeInsets.only(bottom: 300.w),
              child: WebViewWidget(
                controller: controller,
              ),
            ),
            Positioned(
              bottom: 300.w,
              child: Container(
                width: 750.w,
                height: 200.w,
                decoration: const BoxDecoration(
                  gradient: LinearGradient(
                    begin: Alignment.topCenter,
                    end: Alignment.bottomCenter,
                    // Add one stop for each color. Stops should increase from 0 to 1
                    stops: [0.3, 0.9],
                    colors: [
                      Color(0x00ffffff),
                      Config.colorWhite,
                    ],
                    // stops: [0.0, 0.1],
                  ),
                  // color: Colors.yellow,
                ),
              ),
            ),
            Container(
              //顶部标题栏
              decoration: const BoxDecoration(color: Colors.transparent),
              height: 100.w,
              alignment: Alignment.centerLeft,
              margin: EdgeInsets.only(top: 45.w),
              padding: EdgeInsets.symmetric(horizontal: 30.w),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  InkWell(
                    onTap: () {
                      Routes.router.pop(context);
                    },
                    child: Container(
                      padding: EdgeInsets.only(left: 18.w),
                      decoration: BoxDecoration(
                        color: Config.colorBg,
                        borderRadius: BorderRadius.circular(35.w),
                      ),
                      width: 70.w,
                      height: 70.w,
                      child: Icon(
                        Icons.arrow_back_ios,
                        size: 45.w,
                      ),
                    ),
                  ),
                ],
              ),
            ),
            Positioned(
              bottom: 0,
              child: Container(
                width: 700.w,
                height: 700.w,
                margin: EdgeInsets.only(left: 25.w, right: 25.w),
                padding: EdgeInsets.symmetric(horizontal: 20.w),
                decoration: BoxDecoration(
                  color: Config.colorWhite,
                  borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(30.w),
                    topRight: Radius.circular(30.w),
                  ),
                  boxShadow: [
                    BoxShadow(
                      blurRadius: 8, //阴影范围
                      spreadRadius: 3, //阴影浓度
                      color: const Color(0xffdddddd), //阴影颜色
                      offset: Offset(4.w, 8.w),
                    ),
                  ],
                ),
                child: ListView(
                  physics: const BouncingScrollPhysics(),
                  children: [
                    Container(
                      padding: EdgeInsets.symmetric(horizontal: 10.w),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Row(
                            children: [
                              Text(
                                entity.code == 1 ? entity.data.com : '',
                                style: TextStyle(color: Config.colorText1, fontSize: 26.sp),
                              ),
                              Padding(
                                padding: EdgeInsets.only(left: 15.w),
                              ),
                              Text(
                                entity.code == 1 ? entity.data.nu : '',
                                style: TextStyle(color: Config.colorText1, fontSize: 26.sp),
                              ),
                            ],
                          ),
                          Container(
                            margin: EdgeInsets.only(left: 15.w),
                            width: 110.w,
                            height: 38.w,
                            child: ElevatedButton(
                              clipBehavior: Clip.antiAlias,
                              onPressed: () {
                                if (entity.code == 1) {
                                  Clipboard.setData(ClipboardData(text: entity.data.nu));
                                  EasyLoading.showToast("复制成功");
                                }
                              },
                              style: ButtonStyle(
                                elevation: MaterialStateProperty.all(0),
                                backgroundColor: MaterialStateProperty.all(Colors.white),
                                shape: MaterialStateProperty.all(
                                  RoundedRectangleBorder(
                                    borderRadius: BorderRadius.circular(24.w),
                                  ),
                                ),
                                side: MaterialStateProperty.all(
                                  BorderSide(color: Config.colorText3, width: 2.w),
                                ),
                              ),
                              child: Text(
                                '复制',
                                style: TextStyle(fontSize: 18.sp, color: Config.colorText3),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    Divider(
                      thickness: 2.w,
                      height: 40.w,
                      color: Config.colorDivider,
                    ),
                    Container(
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                            width: 40.w,
                            alignment: Alignment.center,
                            child: Column(
                              children: [
                                Container(
                                  width: 13.w,
                                  height: 13.w,
                                  decoration: BoxDecoration(
                                    borderRadius: BorderRadius.all(Radius.circular(10.w)),
                                    color: Config.colorText3,
                                  ),
                                ),
                                Container(
                                  margin: EdgeInsets.symmetric(vertical: 5.w),
                                  width: 1.5.w,
                                  height: 60.w,
                                  decoration: BoxDecoration(
                                    borderRadius: BorderRadius.all(Radius.circular(0.w)),
                                    color: Config.colorText3,
                                  ),
                                ),
                              ],
                            ),
                          ),
                          Container(
                            width: 560.w,
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(
                                  '收货地址：$reciverAddress',
                                  style: TextStyle(
                                      fontSize: 25.sp, height: 1.1, color: Config.colorText3),
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                    ListView.builder(
                      shrinkWrap: true,
                      physics: const NeverScrollableScrollPhysics(),
                      itemCount: entity.data.data.length,
                      scrollDirection: Axis.vertical,
                      itemBuilder: (BuildContext context, int index) {
                        return getListItem(index);
                      },
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
